package com.stone.encrypt.test.infra.handler;

import com.stone.encrypt.app.service.EncryptService;
import org.springframework.stereotype.Component;

/**
 * 加密解密服务处理器
 *
 * @author Mr_wenpan@163.com 2021/08/03 13:35
 */
@Component
public class EncryptServiceHandler implements EncryptService {

    private final String DEFAULT_PUBLIC_KEY = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJL0JkqsUoK6kt3JyogsgqNp9VDGDp+t3ZAGMbVoMPdHNT2nfiIVh9ZMNHF7g2XiAa8O8AQWyh2PjMR0NiUSVQMCAwEAAQ==";

    private final String DEFAULT_PRIMARY_KEY = "MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAkvQmSqxSgrqS3cnKiCyCo2n1UMYOn63dkAYxtWgw90c1Pad+IhWH1kw0cXuDZeIBrw7wBBbKHY+MxHQ2JRJVAwIDAQABAkB3TKXZcVP6tSSN0UgOjLPxng99Z4xvrWJ1jdHFB7TYyUDcnOtE6GkeeMGxtszYtuZ0m5rN9r8eRvIdVUciXbQhAiEA7WGcLd+cvHHA8xNFrbT78Aq4iYm0TVTS6m05ZEOS328CIQCeetcFV8Kdb+P2Y+yWvMtbyFGhROQIFizvxM3S3TiZrQIhAJLrYPB7f9SaSyOm/+89Htk4qXJmyjM6lrgFFgpaUGL9AiB+pkCr/mSDGOYfA+AQ3rPNl5rUvI9XfxFOVAMAntYayQIhAO0hrnwtng6Ubs+58BN69vAl+sd/xugf0jsHcNgZppAb";

    private final String DEFAULT_SECURITY_KEY = "RGarqXE1wpAnW6V5hQs0Lg==";
//
//    private static final String PUBLIC_KEY = "hpfm:encrypt:public-key";
//
//    private static final String PRIVATE_KEY = "hpfm:encrypt:private-key";
//
//    private final RedisHelper redisHelper;
//
//    public EncryptServiceHandler(RedisHelper redisHelper) {
//        this.redisHelper = redisHelper;
//    }

    /**
     * 将公钥保存到Redis中
     *
     * @param publicKey 公钥
     * @author Mr_wenpan@163.com 2021/8/3 5:04 下午
     */
    @Override
    public void savePublicKey(String publicKey) {
//        SafeRedisHelper.execute(HZeroService.Platform.REDIS_DB, redisHelper, () -> {
//            redisHelper.strSet(PUBLIC_KEY, publicKey);
//        });
        return;
    }

    /**
     * 将私钥保存到Redis中
     *
     * @param privateKey 私钥
     * @author Mr_wenpan@163.com 2021/8/3 5:04 下午
     */
    @Override
    public void savePrivateKey(String privateKey) {
//        SafeRedisHelper.execute(HZeroService.Platform.REDIS_DB, redisHelper, () -> {
//            redisHelper.strSet(PRIVATE_KEY, privateKey);
//        });
    }

    @Override
    public String getPublicKey() {
//        String publicKey = SafeRedisHelper.execute(HZeroService.Platform.REDIS_DB, redisHelper,
//                () -> redisHelper.strGet("hpfm:encrypt:public-key"));
//        if (StringUtils.isBlank(publicKey)) {
//            throw new CommonException("hpfm.warn.encrypt.public-key is empty");
//        }
//        return publicKey;
        return DEFAULT_PUBLIC_KEY;
    }

    @Override
    public String getPrivateKey() {
//        String privateKey = SafeRedisHelper.execute(HZeroService.Platform.REDIS_DB, redisHelper,
//                () -> redisHelper.strGet("hpfm:encrypt:private-key"));
//        if (StringUtils.isBlank(privateKey)) {
//            throw new CommonException("hpfm.warn.encrypt.private-key is empty");
//        }
//        return privateKey;
        return DEFAULT_PRIMARY_KEY;
    }

    @Override
    public String getSecurityKey() {
        return DEFAULT_SECURITY_KEY;
    }

}
